<?php

/*
 * 不良品处理方法
 */

class qualitycomm
{
    //根据不良品处理表的ID来选择不良品出库表
    public static function select_bad_product_delivery_byid($id)
    {
        return $db = DB::select()
            ->from('bad_product_delivery')
            ->where('unqualified_id','=',$id)
            ->execute('newerp')
            ->as_array();
    }

    //根据不良品出库表的ID来更新记录
    public static function update_bad_product_delivery_byid($id,$array)
    {
        $coupon_price = $array['coupon_price'];
        $return_cost = $array['return_cost'];
        $sql = "update bad_product_delivery set quantity = '".$array['quantity']."',"." fail_deal_way = '".$array['fail_deal_way']."',deal_time = '".$array['deal_time']."',"." unqualified_reason = '".$array['unqualified_reason']."',coupon_price='$coupon_price',return_cost='$return_cost' where id = $id";
        //		echo json_encode($sql);exit;
        return $db = DB::query(Database::UPDATE,$sql)
            ->execute('newerp');
        //			$db=DB::update('bad_product_delivery')
        //		->set($array)
        //		->where('id','=',$id)
        //		->execute('newerp');
    }

    //查询不良品处理的质检报告
    public static function qualityinfo($where,$fields = '*')
    {
        $select_sql = 'SELECT '.$fields.' FROM bad_product_delivery WHERE '.$where;
        $result     = DB::query(Database::SELECT,$select_sql)
            ->execute('newerp')
            ->as_array();
        return $result;
    }

    //更新采购明细的不合格数量
    public static function update_purchaseorder_details_quantity($sku,$purchaseorder_id,$num)
    {
        return $r = DB::update('purchaseorder_details')
            ->set(array('return_quantity' => $num))
            ->where('sku','=',$sku)
            ->where('purchaseorder_id','=',$purchaseorder_id)
            ->execute('newerp');
    }

    // 更新质检明细不合格量和合格量//taj8-1只新增质检数据，不修改原数据
    public static function update_details_quantity($checkid,$count)
    {
        /*$set = 'qualified_quantity = qualified_quantity + '.$count;
        $set .= ',unquality_count = unquality_count - '.$count;
        $pur_query = "UPDATE check_quality_details SET ".$set." WHERE id = ".$checkid;
        $id = DB::query(Database::UPDATE,$pur_query)->execute('newerp');
        return $id;*/
        $old_cc                    = ORM::factory('check_quality_detail',$checkid);
        $in_cc                     = ORM::factory('checkqualitydetails');
        $in_cc->invoice_date       = date("Y-m-d H:i:s");
        $in_cc->check_man          = User::active_user()->id;
        $in_cc->purchaseorder_id   = $old_cc->purchaseorder_id;
        $sql = 'SELECT warehouseid FROM purchaseorders WHERE id='.$in_cc->purchaseorder_id;
        $warehouse_id = DB::query(Database::SELECT,$sql)->execute('newerp')->get('warehouseid');
        $in_cc->recieve_detail_id  = $old_cc->recieve_detail_id;
        $in_cc->supplier_id        = $old_cc->supplier_id;
        $in_cc->sku                = $old_cc->sku;
        $in_cc->sku_name           = $old_cc->sku_name;
        $in_cc->sku_standard       = $old_cc->sku_standard;
        $in_cc->storage_position   = $old_cc->storage_position;
        $in_cc->qualified_quantity = $count;
        $in_cc->check_quantity     = $count;
        $in_cc->fail_deal_way      = 5;
        $in_cc->save();
        if($in_cc->saved()){
            return 'ok';

        }else{
            return 'no';
        }
    }

    //查询采购明细的不合格数量
    public static function selcet_purchaseorder_details_quantity($sku,$purchaseorder_id)
    {
        return $db = DB::select(array(
            DB::expr('sum(return_quantity)'),
            'num'
        ))
            ->from('purchaseorder_details')
            ->where('sku','=',$sku)
            ->where('purchaseorder_id','=',$purchaseorder_id)
            ->execute('newerp')
            ->current();
    }

    //根据不良品出库表的ID来查询不良品出库表的该记录的状态
    public static function select_bad_product_delivery($colom,$id)
    {
        return $db = DB::select($colom)
            ->from('bad_product_delivery')
            ->where('id','=',$id)
            ->execute('newerp')
            ->current();
    }

    public static function getunqualityid($id)
    {
        $select_sql = "SELECT purchase_man FROM unqualified_deal_invoices WHERE id = '".$id."'";
        $res        = DB::query(Database::SELECT,$select_sql)
            ->execute('newerp')
            ->as_array();
        if(empty($res)){
            return 0;
        }else{
            return $res['0']['purchase_man'];
        }
    }
}
